package com.chixing.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class FileUploadController {
    @RequestMapping("upload")
    public String upload(MultipartFile myfile) {
        System.out.println("上传的文件是：getOriginalFilename:"+myfile.getOriginalFilename());
        System.out.println("上传的文件是：getName :" + myfile.getName());

        //1、重命名文件
        String fileName = UUID.randomUUID().toString().replace("-","");
        String fileOriginalName[] = myfile.getOriginalFilename().split("\\.");
        String suffix = fileOriginalName[fileOriginalName.length-1];

        fileName = fileName + "." + suffix;
        String filePathName = "D:\\java\\Java_Notes\\upload\\" + fileName;

        //2、上传

        try {
            myfile.transferTo(new File(filePathName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "a";
    }
}
